<template>
  <view class="container">
    <view style="width: 100%; display: flex; align-items: center; justify-content: center">
      <button style="margin: 30px auto; background-color: #c3c3c3; display: flex; align-items: center; justify-content: center; height: 100px; color: #fff" v-on:click="scan">
        <u-icon name="scan" color="#fff" size="40"></u-icon>
        扫一扫
      </button>
    </view>

    <view style="display: flex; align-items: center; padding: 20rpx">
      <li-input v-model="code" style="flex: 1" />
      <view class="btn-box">
        <button type="primary" size="mini" v-on:click="yq">券码验券</button>
      </view>
    </view>
  </view>
</template>

<script>
import { uNavigateTo, uScanCode, uSetStorageSync, uRemoveStorageSync } from '../../../utils/uniUtils';
import { baseURLWebApi } from '../../../utils/env';

export default {
  data() {
    return {
      code: '108305293547426'
    };
  },

  mounted() {
    uRemoveStorageSync('shortLinkTiktok');
  },
  methods: {
    async scan() {
      const res = await uScanCode();
      uSetStorageSync('shortLinkTiktok', res);
      uNavigateTo(`/pages/workbench/GroupPurVerify/tiktokVerify`);
    },
    async yq() {
      uni.request({
        url: `${baseURLWebApi}/api/tiktok/CertificatePrepareByCode`,
        data: {
          data: this.code
        },
        method: 'POST',
        success(res) {
          let {
            data: { error_code, order_id, verify_token, certificates }
          } = res;

          if (error_code == 0) {
            this.certificates = certificates;
            this.form.verify_token = verify_token;
            this.form.certificates.push(certificates[0].encrypted_code);
          }
        }
      });
    }
  }
};
</script>
<style scoped lang="scss">
.btn-box {
  width: 23%;
  margin-left: 20rpx;
}
</style>
